/**
 * Author: Andrew Shore
 * Date Last Modified: April 14, 2009
 * CSCE 146: Homework 07, PacketAssembler.java
 * 
 */
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;


public class PacketAssembler {

	/**
	 * Class variables for PacketAssembler class
	 */
  static final String classLabel = "PacketAssembler: ";
  private TreeMap<Integer,TreeMap<Integer,Packet>> allMessages;
  private TreeMap<Integer, Integer> packetCountRead;
  private TreeMap<Integer, Packet> oneMessage;
  
	/**
	 * Default Constructor for PacketAssembler, initializes treeMaps
	 */
  public PacketAssembler(){
	 // oneMessage=new TreeMap<Integer, Packet>();
	  allMessages = new TreeMap<Integer, TreeMap<Integer, Packet>>();
	  packetCountRead = new TreeMap<Integer, Integer>();
  }
  
  /**
   * Method reads packets from a file and puts them in a tree map with the 
   * messageID as the key and another tree map with key of packetId and 
   * value of Packet, method also checks to see if the message is complete
   * and return's the messageID if it is
   */
	
	public int readPackets(Scanner inFile) {
		
		while(inFile.hasNext()){
			
			Packet thePacket = new Packet();
			thePacket = thePacket.readPacket(inFile);
			
			/*if there are no entries for the message then it is added and
				packetCount is set to one for that message
			*/
			if(!packetCountRead.containsKey(thePacket.getMessageID())){
				
				oneMessage = new TreeMap<Integer,Packet>();
				oneMessage.put(thePacket.getPacketID()	, thePacket);
				allMessages.put(thePacket.getMessageID(), oneMessage);
				packetCountRead.put(thePacket.getMessageID(), 1);
				
			}else{
				/*	Checks to see if the packetID is already in the message tree, no
				 * 		duplicates are allowed  
				 */
				if(!allMessages.get(thePacket.getMessageID()).containsKey(
						             thePacket.getPacketID())){
					
				allMessages.get(thePacket.getMessageID()).put(thePacket.getPacketID(),
						         thePacket);
				
				allMessages.put(thePacket.getMessageID(), 
						         allMessages.get(thePacket.getMessageID()));
				
				packetCountRead.put(thePacket.getMessageID(),
						             packetCountRead.get(thePacket.getMessageID())+1);
				
			}
			}
			
			//	Checks to see if the message is complete and if it is returns the messageID
		if(packetCountRead.get(thePacket.getMessageID())==thePacket.getPacketCount()){
			FileUtils.logFile.println(classLabel+"	done with message	"
					+ thePacket.getMessageID()+
					"	" +	thePacket.getPacketCount());
			return thePacket.getMessageID();
		}else{
			return -1;
		}
		
		}
		return -1;
	}

	/**
	 * Method prints the contents of the sorted tree to the logFile by transferring 
	 * the keys to a set than iterating over that set and printing the Packet 
	 * that corresponds with that key. The int parameter is the messageID of the 
	 * message to be printed
	 */
	public void writePackets(int i) {
		//gets the tree associated with the message to be printed
		Set<Integer> theTree = allMessages.get(i).keySet();
		int theKey;
		Iterator<Integer> iterate = theTree.iterator();
		
		while(iterate.hasNext()){
			theKey = iterate.next();
			FileUtils.logFile.println(classLabel + "	" + theKey + "	'	" + 
					                  allMessages.get(i).get(theKey).getMessageID()+
					                  "	" + allMessages.get(i).get(theKey).toString());
			
		}//while(iterate.hasNext())
		
		//removes the tree once it is printed
		allMessages.remove(i);
		FileUtils.logFile.flush();
		
	}

}
